require("Script.Common.Debug")


local PlayerMoveController = {
    -- 移动方向
    Direction = 0; --枚举类型
    -- 最终目标砖块
    TargetTile = nil;
    -- pawn是否正在移动
    IsPawnMoving = false;
}; 

-- 移动至指定砖块,服务端执行
function PlayerMoveController:MoveTargetTile(direction,targettile)
    -- log added. v_sbbsxia asked for checking if jumping or not
    print("v_sbbsxia : AeroplaneAIController:Jump Start 1")
    self.Direction = direction
    self.TargetTile = targettile
    print("MoveTargetTile"..direction)
    print("MoveTargetTile"..targettile.ID)
end

function PlayerMoveController:ReceiveTick(DeltaTime)
    if self:HasAuthority() then
        local curTile = self:K2_GetPawn():GetCurTile()
        Debug:Log("PlayerMoveController:ReceiveTick",self:K2_GetPawn():GetCurTile())
        --print(string.format("curTileqqqqqqq[%d] self.TargetTile.Index[%d]",curTile.Index,self.TargetTile.Index))
        if curTile ~= nil and self.TargetTile ~= nil and curTile.ID ~= self.TargetTile.ID then
            self:DoNormalWalk()
        end

        
        self:CheckMovementFinished()
    end
end

--currenttile是砖块的pawn
function PlayerMoveController:ReturnNextTileIndex(currenttile,direction)
    if direction == 1 then return currenttile.LeftUp end
    if direction == 2 then return currenttile.Left end
    if direction == 3 then return currenttile.LeftDown end
    if direction == 4 then return currenttile.RightUp end
    if direction == 5 then return currenttile.Right end
    if direction == 6 then return currenttile.RightDown end
end

--移动一格
function PlayerMoveController:DoNormalWalk()
    if not self.IsPawnMoving then
        -- 获得角色现在所在的砖块
        local curTile = self:K2_GetPawn():GetCurTile()

        if curTile == nil then
            print("AeroplaneAIController:DoNormalWalk curTile is nil")
            return false
        end
        -- 一次一格移动的目标砖块Index
        local nextTile = self:ReturnNextTileIndex(curTile,self.Direction)
        local targetTile = nextTile
        if targetTile and UE.IsValid(targetTile) then
            local endLoc = nextTile.Pos:K2_GetComponentLocation()
            self.targetPosition = endLoc
            self.NextTile = nextTile
            self.IsPawnMoving = true

            -- 移动后延迟一会儿，标记destroyed和碎裂

            local PlayCardTimerDelegate = ObjectExtend.CreateDelegate(self, curTile,
                function(curTile)
                    curTile.isDestroyed = true;
                    curTile:DestroyTile();-- 暂时在移动中就调用删除砖块
                end
            )

           

            -- 移动到下个地砖的位置，棋子相关的代码？？？？？？？？？？？？待完善
            -- VectorHelper需要添加到代码文件中
            print("MoveToLocation start")
            self:MoveToLocation(VectorHelper.ToLuaTable(endLoc))
            print("MoveToLocation end")
            return true
        else
            print("AeroplaneAIController:DoNormalWalk Invalid Target Tile")
        end
    end
    return false
end

function PlayerMoveController:CheckMovementFinished(DeltaTime)
    if not self.IsPawnMoving then
        return
    end

    local DistToDestLocation = VectorHelper.GetDistance2D(self:K2_GetPawn():K2_GetActorLocation(), self.targetPosition)
    if DistToDestLocation <= 100 then
        -- 移动到了位置
        self.IsPawnMoving = false
        -- 记录上一个格子
        local LastTileIndex = self:K2_GetPawn().CurrentTileIndex;
        table.insert(UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete,LastTileIndex)
        -- 给角色设置新的当前砖块Index
        self:K2_GetPawn().CurrentTileIndex = self.NextTile.ID
        print(string.format("nextidjskdjlk[%d]",self.NextTile.ID))
        -- 将上一个格子的角色位置根据角色数量重置
        --UGCGameSystem.GameState:ResetTilePawn(LastTileIndex);
        local curTile = self:K2_GetPawn():GetCurTile()
        if curTile == nil then return end
        -- 正在进行正常移动
        if curTile.ID == self.TargetTile.ID then
            self:FinishMove()
        end
    end
end

function PlayerMoveController:FinishMove()
    --关于执行的问题？？？？？？？？网络同步
    print("dskgwifsdf")

    local TileDeleteTimerDelegate = ObjectExtend.CreateDelegate(self,
         function()
         end
     )
    print(" numTileWaitForDelete"..#UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete)--一次为3
    for i=1,#UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete do
        local deletetileid = UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete[i]
        --KismetSystemLibrary.K2_SetTimerDelegateForLua(TileDeleteTimerDelegate, self, 2, false)
        print("TileDeleteTimerDelegate is")--只执行一个时间,卡在这儿
        UGCGameSystem.GameState.TileList[deletetileid]:DestroyTile()
        UGCGameSystem.GameState.TileList[deletetileid].isDestroyed = true;
        print("TileDeleteTimerDelegate ii")
    end
    UGCGameSystem.GameState.PlayerInfos[UGCGameSystem.GameState.CurRoundTeamIndex].TileWaitForDelete = {}
    --UGCGameSystem.GameState:AddCard(UGCGameSystem.GameState.PlayerInfos[self.CurRoundTeamIndex])
    -- 通知服务器已经移动完毕,结束这个回合
    print("FinishMove success here.")
    --LuaQuickFireEvent("PlayerRoundEnd", self)
    UGCGameSystem.GameState:PlayerFinishedMove();
    -- 恢复相机到顶部
    UGCGameSystem.GameState:SetAllPlayerFocusToTopCamera();
end

--[[
-- 根据要去的格子上面有多少个玩家确定在格子上的站位
function PlayerMoveController:GetMoveTargetPosByTilePawnNum(Tile,TilePawnNum)
    if TilePawnNum == 0 then
        return Tile:GetOnePlayerLocation()
    elseif TilePawnNum == 1 then
        return Tile:GetTwoPlayerTileLocation(2)
    elseif TilePawnNum == 2 then
        return Tile:GetThreePlayerTileLocation(3)
    elseif TilePawnNum == 3 then
        return Tile:GetFourPlayerTileLocation(4)
    elseif TilePawnNum == 4 then
        return Tile:GetOnePlayerLocation()
    end
end
]]--

return PlayerMoveController;